package com.simon.resourcestatistics.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.simon.resourcestatistics.dto.MemberDTO;
import com.simon.resourcestatistics.model.Member;
import com.simon.resourcestatistics.service.IMemberService;
import com.simon.resourcestatistics.util.POIUtil;

import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * MemberController
 */
@ApiOperation("会员列表")
@RestController
@RequestMapping("/member")
public class MemberController {

    @Resource
    private IMemberService memberService;

    @ApiOperation(value="分页查询",notes="分页查询")
    @PostMapping("/findPage")
    public Page<Member> findPage(@RequestBody  MemberDTO dto){
        return memberService.findPage(dto);
    }

    @ApiOperation(value="保持会员信息",notes="添加或者修改")
    @PostMapping
    public Member saveMember(@RequestBody  Member m){
        return memberService.saveMember(m);
    }

    @ApiOperation(value = "批量添加", notes="pl")
    @PostMapping(value="/batch")
    public int batchAdd(){
     //path参数是前台页面上传时的文件名，这里使用本地文件模拟下
     final String path="C:\\Users\\86135\\Desktop\\会员登记表.xlsx";
     List<Member> list=new ArrayList<Member>();
     try{
         list=POIUtil.impoMembers(path);
     }catch(final IOException e){
         e.printStackTrace();
     }
     return memberService.addAll(list);
    }
    
    @ApiOperation(value="删除",notes="delete")
    @ApiImplicitParam(name="id",value="主键",required=true,paramType = "path")
    @DeleteMapping("/{id}")
    public void del(@PathVariable  Integer id){
        memberService.del(id);
    }

   @ApiOperation(value="批量删除",notes = "deletes")
   @DeleteMapping
   public void deletes(@RequestParam("ids") String ids){
       final String[] str=ids.split(",");
       final List<Integer> list=new ArrayList<>();
       for(final String l : str){
           final Integer i=Integer.valueOf(l);
           list.add(i);
       } 
       memberService.deleteMember(list);
   }

}